//app.js
App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    var that = this;
    var openid = wx.getStorageSync('openid');
    if (!openid){
      wx.login({
        success: function (res) {
          console.log(res)
          if (res.code) {
            wx.request({
              url: 'https://apistore.hy9z.com/wechatxcx/openid',
              method: "post",
              data: {
                grant_type: 'authorization_code',
                js_code: res.code,
                appid: 'wx192188f2ba3f77cc',
                secret: '6445d2962d97200495b58e6e93f8c253'
              },
              success: function (reasult) {
                console.log(reasult.data)
                if (reasult.data.code == 0) {
                  var openid = JSON.parse(reasult.data.data).openid;

                  wx.setStorage({
                    key: "openid",
                    data: openid
                  })
                }
              }
            })
          }else{

            console.log('获取用户登录态失败！' + res.errMsg)
        
          }
        }
      })    
    }
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.getUserInfo({
        withCredentials: false,
        success: function (res) {
          that.globalData.userInfo = res.userInfo
          typeof cb == "function" && cb(that.globalData.userInfo)
        }
      })
    }
  },

  globalData: {
    userInfo: null,
    openid: null
  }
})
